﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace ComboBox
{
    public partial class ComboBox : Form
    {
        public ComboBox()
        {
            InitializeComponent();
            Autorzy = new ArrayList();
            wypelnij();
        }
        
        void wypelnij()
        {
            Autorzy.Add(new AddValue("text1", 1));
            Autorzy.Add(new AddValue("text2", 2));
            Autorzy.Add(new AddValue("text3", 3));

            comboBox1.DisplayMember = "Text";
            comboBox1.ValueMember = "Liczba";
            comboBox1.DataSource = Autorzy;
        }
        ArrayList Autorzy;

        private void button1_Click(object sender, EventArgs e)
        {
            comboBox1.DataSource = null;
            Autorzy.Clear();
            tbWynik.Clear();
            //comboBox1.Items.Clear();

            //MessageBox.Show(Autorzy.
        }

        private void btDodaj_Click(object sender, EventArgs e)
        {
            comboBox1.DataSource = null;
            Autorzy.Add(new AddValue(nowyText.Text,Convert.ToInt32(nowaLiczba.Text)));
            comboBox1.DisplayMember = "Text";
            comboBox1.ValueMember = "Liczba";
            comboBox1.DataSource = Autorzy;
        }

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            //tbWynik.Text = comboBox1.SelectedValue.ToString();
        }

        private void comboBox1_DropDownClosed(object sender, EventArgs e)
        {
            try
            {
                tbWynik.Text = comboBox1.SelectedValue.ToString();
            }
            catch (NullReferenceException exp)
            {
                tbWynik.Text = "";
            }
        }
    }
    public class AddValue
    {
        private long liczba;
        private string text;

        public AddValue(string text, long liczba)
        {
            this.liczba = liczba;
            this.text = text;
        }
        public long Liczba
        {
            get { return liczba; }
        }
        public string Text
        {
            get { return text; }
        }
    }
}
